<?php

namespace SimpleFactoryAndStrategyGroup;

use SimpleFactoryAndStrategyGroup\Strategy\ {ConcreteStrategyA, ConcreteStrategyB, ConcreteStrategyC};

include_once "StrategyInterface.php";
include_once "Strategy/ConcreteStrategyA.php";
include_once "Strategy/ConcreteStrategyB.php";
include_once "Strategy/ConcreteStrategyC.php";

/** 策略模式的上下文类 */
class Context
{
    /** @var string 实体类标识 */
    const STRATEGY_A = 'A';
    const STRATEGY_B = 'B';
    const STRATEGY_C = 'C';
    public array $strategyClass = [
        self::STRATEGY_A => ConcreteStrategyA::class,
        self::STRATEGY_B => ConcreteStrategyB::class,
        self::STRATEGY_C => ConcreteStrategyC::class,
    ];
    /** @var StrategyInterface 存储策略对象 */
    public StrategyInterface $strategyObject;

    /**
     * 初始化 $strategyObject
     * @param string $strategy 实体类标识
     */
    public function __construct(string $strategy)
    {
        $this->strategyObject = new $this->strategyClass[$strategy]();
    }

    /**
     * 策略模式统一执行具体策略类的方法
     * @return mixed
     */
    public function contextStrategy()
    {
        return $this->strategyObject->algorithm();
    }
}